﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kosher;
using Ninject;
using Timetable.Base.Entities;
using Timetable.Base.Interfaces.Repositories;
using Timetable.Base.Interfaces.Validators;

namespace Timetable.Data.Validators
{
    public class ScheduleAttributeValidator : Validator<ScheduleInfo, string>, IValidator<ScheduleInfo>
    {
        public IEnumerable<object> GetErrors()
        {
            return this.Errors.AsEnumerable();
        }

        [Inject]
        public IRepository<ScheduleInfo> Repository { get; set; }

        #region Overrides of Validator<ScheduleInfo,string>

        protected override void Setup()
        {
            CheckThat(x => x, "ScheduleAttr")
                .Passes(x => {
                    var exist = Repository.DataSource
                        .ScheduleInfos
                        .Any(y => 
                               y.Lecturer.Id.Equals(x.Lecturer.Id)
                            && y.Tutorial.Id.Equals(x.Tutorial.Id)
                            && y.TutorialType.Id.Equals(x.TutorialType.Id)
                            //&& y.Faculties.Any(
                            //        z => x.Faculties.Any(
                            //           k => k.Id.Equals(z.Id)
                            //        )
                            //    )
                            //&& y.Courses.Any(
                            //        z => x.Courses.Any(
                            //            k => k.Id.Equals(z.Id)
                            //        )
                            //    )
                            //&& y.Groups.Any(
                            //        z => x.Groups.Any(
                            //            k => k.Id.Equals(z.Id)
                            //        )
                            //    )
                            //&& y.Specialities.Any(
                            //        z => x.Specialities.Any(
                            //            k => k.Id.Equals(z.Id)
                            //        )
                            //    )
                        );
                    return !exist;
                })
                .Otherwise(Resources.Messages.This_schedule_attrs_already_exist);
        }

        #endregion
    }
}
